package com.leetcode.LC;

import java.util.Arrays;

public class LC350 {
    public int[] intersect(int[] nums1, int[] nums2) {

        int[] ints = new int[nums1.length > nums2.length ? nums2.length : nums1.length];
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int a = 0, b = 0, count = 0;
        while (a < nums1.length && b < nums2.length) {
            if (nums1[a] == nums2[b]) {
                ints[count] = nums1[a];
                count++;
                a++;
                b++;
            } else if (nums1[a] < nums2[b]) {
                a++;
            } else if (nums1[a] > nums2[b]) {
                b++;
            }
        }
        return Arrays.copyOf(ints, count);
    }

    public static void main(String[] args) {
        for (int i : new LC350().intersect(new int[]{4, 9, 4, 5}, new int[]{9, 4, 9, 8, 4})) {
            System.out.print(i + ",");
        }
    }
}
